<?php

namespace App\Http\Controllers\Home;

use App\models\Collect;
use App\models\Goods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CollectController extends CommonController
{
//    收藏和取消收藏的方法
    public function make(Goods $good,Collect $collect)
    {
//       dd(123);
        $collect->user_id = auth()->id();
        $collect->good_id = $good->id;
//        首先判断当前用户是否收藏该商品
        $status = Collect::where('user_id',auth()->id())->where('good_id',$good->id)->first();
//        dd($status);
        if (!$status)
        {
//        写入收藏表
          $collect->save();
          return back()->with('success','收藏成功');
        }else{
           $status->delete();
           return back()->with('success','取消收藏成功');
        }
    }

    public function myCollect()
    {
        $collects = Collect::where('user_id',auth()->id())->get();
        return view('home.center_collect',compact('collects'));
    }
}
